package dao.server;

import java.net.ServerSocket;
import java.net.Socket;

public class Communicate extends Thread {
	private ServerSocket wellcomeSocket;
	private DAO dao;

	public Communicate(ServerSocket wellcomeSocket, DAO dao) {
		this.wellcomeSocket = wellcomeSocket;
		this.dao = dao;
	}

	public void run() {
		try {
			while (true) {
				Socket connectionSocket = wellcomeSocket.accept();
				User user = new User(connectionSocket);
				user.setDAO(dao);
				Thread t = new Thread(user);
				t.start();
			}
		} catch (Exception e) {			
			try{
				wellcomeSocket.close();
				System.out.println("Close success");
				dao.closeConnection();
			}catch(Exception ex){
				System.out.println("Could not close socket");				
			}			
		}
	}
}
